package kevin.framework.basic.common.user;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import kevin.framework.basic.common.thread.RequestThreadHolder;

/**
 * @ClassName: SessionUserContext
 * @Description: 利用httpSession保存登录用户信息，用于单机版本部署
 * @author kevin.huang
 * @date 2016年1月13日 下午9:17:59
 */
public class SessionUserContext extends LoginUserContext implements ILoginUserContext {

	public final Logger logger = org.slf4j.LoggerFactory.getLogger(SessionUserContext.class);

	@Override
	public void setExpiry(int expiry) {
		this.expiry = expiry;
	}

	@Override
	public int getExpiry() {
		return this.expiry;
	}

	@Override
	public void setUpdateRate(int rate) {
		this.updateRate = rate;
	}

	/***
	 * 实现登录session的平滑延时
	 ***/
	@Override
	public void updateExpiry(String key) throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return;
		}
		Object user = session.getAttribute(key);
		if (user != null) {
			LoginUser u = (LoginUser) user;
			Date now = new Date();
			long diff = now.getTime() - u.getSessionTime().getTime();
			long min = diff / (1000 * 60);
			long flag = this.expiry / this.updateRate;
			if (min >= flag) {
				session.removeAttribute(key);
				u.setSessionTime(now);
				session.setAttribute(key, user);
			}
		}
	}

	@Override
	public String setLoginUser(LoginUser user) throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return null;
		}
		String key = DigestUtils.md5Hex(user.getId() + "_" + user.getLoginIp());
		session.setAttribute(key, user);
		user.setUserKey(key);
		return key;
	}

	@Override
	public void logout(String key) throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return;
		}
		if (key != null) {
			session.removeAttribute(key);
			session.invalidate();
		}
	}

	@Override
	public void logout() throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return;
		}
		String key = this.getLoginKeyFromRequest();
		if (key != null) {
			session.removeAttribute(key);
			session.invalidate();
		}

	}

	@Override
	public void setKeyValue(String key, Object obj) throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return;
		}

		session.setAttribute(key, obj);
	}

	@Override
	public Object getKeyValue(String key) throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return null;
		}
		return session.getAttribute(key);
	}

	@Override
	public LoginUser getCurrentUser() throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return null;
		}
		String key = this.getLoginKeyFromRequest();
		if (key == null) {
			HttpServletRequest ra = getRequest();
			if (ra != null) {
				Object sessionKey = ra.getAttribute("invt_sessionkey");
				if (sessionKey != null) {
					key = String.valueOf(sessionKey);
				}
			}
			if (key == null) {
				return null;
			}
		}
		Object obj = session.getAttribute(key);
		if (obj == null) {
			return null;
		}

		return (LoginUser) obj;
	}

	@Override
	public void deleteKeyValue(String key) throws Exception {
		HttpSession session = getSession();
		if (session == null) {
			return;
		}
		session.removeAttribute(key);
	}
}
